{include file="public/header" /}
<body class="gray-bg">
<div class="wrapper wrapper-content">
    <div class="ibox-title">
        <h5>添加广告图</h5>
    </div>
    <div class="ibox-content layui-form">
        <div class="layui-row layui-col-space10 layui-form-item ">
            <input type="hidden" name="id" value="{$data.ad_id}">
            <div class="layui-form-item layui-col-md-offset1 layui-col-md8">
                <label class="layui-form-label">广告图：</label>
                <input type="hidden" name="img" id="lay-img" lay-verify="required" value="{$data.ad_img}">
                <div class="layui-input-block">
                    <div id="lay-upload">上传图片</div>
                    <span>建议尺寸：4:3</span>
                    <blockquote class="layui-elem-quote layui-quote-nm" style="margin-top: 10px;">
                        预览图：
                        <div class="layui-upload-list" id="lay-list">
                            <img class="layui-append-img" src="{$data.ad_img}" onerror="this.src='/static/admin/images/no_img.jpg'">
                        </div>
                    </blockquote>
                </div>
            </div>
            <div class="layui-form-item layui-col-md-offset1 layui-col-md8">
                <label class="layui-form-label">终端：</label>
                <div class="layui-input-block">
                    <select name="port" lay-search="" lay-verify="required" lay-filter="port">
                        <option  value="">选择终端</option>
                        <option  value="1" {eq name="$data.ad_port" value="1"}selected{/eq}>用户首页</option>
                        <option  value="2" {eq name="$data.ad_port" value="2"}selected{/eq}>商户首页</option>
                        <option  value="3" {eq name="$data.ad_port" value="3"}selected{/eq}>社交圈</option>
                    </select>
                </div>
            </div>
            <div class="layui-form-item layui-col-md-offset1 layui-col-md8" {eq name="$data.ad_port" value="2"}style="display: none;"{/eq}  id="cate">
                <label class="layui-form-label">链接类型：</label>
                <div class="layui-input-block">
                    <select name="cate" lay-search="" lay-verify="required" lay-filter="cate" id="cate1">
                        <option  value="">选择链接类型</option>
                        <option  value="1" {eq name="$data.ad_cate" value="1"}selected{/eq}>外部链接</option>
                        <option  value="2" {eq name="$data.ad_cate" value="2"}selected{/eq}>店铺</option>
                        <option  value="3" {eq name="$data.ad_cate" value="3"}selected{/eq}>商品</option>
                        <option  value="4" {eq name="$data.ad_cate" value="4"}selected{/eq}>积分活动</option>
                        <option  value="5" {eq name="$data.ad_cate" value="5"}selected{/eq}>动态</option>
                    </select>
                </div>
            </div>
            {$data.ad_cate}
            <div class="layui-form-item layui-col-md-offset1 layui-col-md8" {if $data.ad_port == 2 && $data.ad_cate != 1}style="display: none;"{/eq} id="link">
                <label class="layui-form-label">链接：</label>
                <div class="layui-input-block" id="change">
                    {if condition="$data.ad_cate == 1"}
                    <input type="text" name="link" lay-verify="required" placeholder="链接" class="layui-input " id="link1" value="{$data.ad_link}">
                    {else /}
                    <select name="link" lay-search="" lay-verify="required" id="link1">
                        <option value="">请选择</option>
                        {if !empty($data.link)}
                        {foreach $data.link as $vo}
                        <option value="{$vo.id}" {eq name="$data.ad_link" value="$vo.id"}selected{/eq}>{$vo.name}</option>
                        {/foreach}
                        {/if}
                    </select>
                    {/if}

                </div>
            </div>
            <div class="layui-form-item layui-col-md-offset1 layui-col-md8" {in name="$data.ad_port" value="2,3"}style="display: none;"{/in} id="pos">
                <label class="layui-form-label">位置：</label>
                <div class="layui-input-block">
                    <select name="place" lay-search="" lay-verify="{eq name='$data.ad_port' value='1'}required{/eq}" id="place">
                        <option  value="">选择广告位置</option>
                        <option  value="1" {eq name="$data.ad_postion" value="1"}selected{/eq}>上部</option>
                        <option  value="2" {eq name="$data.ad_postion" value="2"}selected{/eq}>下部</option>
                    </select>
                </div>
            </div>
            <div class="layui-form-item layui-col-md-offset1 layui-col-md8">
                <label class="layui-form-label">左标题：</label>
                <div class="layui-input-block">
                    <input type="text" name="left_title" lay-verify="required" placeholder="左标题" class="layui-input " maxlength="4" value="{$data.ad_left_title}">
                </div>
            </div>
            <div class="layui-form-item layui-col-md-offset1 layui-col-md8">
                <label class="layui-form-label">人数：</label>
                <div class="layui-input-block">
                    <input type="text" name="right_title" lay-verify="required" placeholder="推选人数" class="layui-input " value="{$data.ad_right_title}" oninput="value=value.replace(/[^\d]/g,'')">
                </div>
            </div>
            <div class="layui-form-item layui-col-md-offset1 layui-col-md8">
                <div class="layui-input-block">
                    <button class="layui-btn" lay-submit="" lay-filter="component-form-element">保存
                    </button>
                    <button class="layui-btn layui-btn-primary" onclick="history.go(-1)">返回</button>
                </div>
            </div>
        </div>
    </div>
</div>
{include file="public/footer" /}
<script>
    wk.uploadImg({size:5,type:'jpg,jpeg,png',url:"{:url('Upload/uploadLocality')}"});
    layui.use(['form'], function() {
        var form = layui.form;

        form.on('select(port)', function(data){
            if(data.value == 2){
                $('#link').show();
                $('#link1').attr('lay-verify','required');
                $("#change").empty().append('<input type="text" name="link" lay-verify="required" placeholder="链接（https://www.xxx.com）" class="layui-input " id="link1">');
                $('#cate').hide();
                $('#cate1').attr('lay-verify','');
                $('#pos').hide();
                $('#place').attr('lay-verify','');
            }else{
                $("#cate").show()
                $('#cate1').attr('lay-verify','required').val('');
                if(data.value == 1){
                    $("#pos").show()
                    $('#place').attr('lay-verify','required').val('');
                }else{
                    $('#pos').hide();
                    $('#place').attr('lay-verify','');
                }
                $('#link').hide()
                $('#link1').attr('lay-verify','');
            }
            form.render();
        });
        form.on('select(cate)', function(data){
            $('#link').show();
            if(data.value == 1){
                $('#link1').attr('lay-verify','required');
            }else{
                $.ajax({
                    url:"{:url('adCate')}",
                    type:"post",
                    dataType:"json",
                    data:{cate:data.value},
                    success:function (res) {
                        var what = "";
                        switch(data.value){
                            case '2':
                                what = "店铺";
                                break;
                            case '3':
                                what = "商品";
                                break;
                            case '4':
                                what = "积分活动";
                                break;
                            case '5':
                                what = "动态"
                                break;
                        }
                        var opt = '<option value="">请选择'+what+'</option>';
                        $.each(res.msg,function(key,vo){
                            opt += "<option value="+vo.id+">"+vo.name+"</option>";
                        })
                        $("#change").empty().append('<select name="link" lay-search="" lay-verify="required" id="link1">'+opt+'</select>')
                        form.render();
                    }
                })
            }
        });

        form.on('submit(component-form-element)', function (data) {
            $('.layui-btn').addClass('layui-disabled').attr('disabled','disabled');
            $.ajax({
                url:"{:url('adEdit')}",
                type:'post',
                dataType:'json',
                data:data.field,
                success:function(res){
                    if (res.code == 200) {
                        wk.success(res.msg,"{:url('Advertise/index')}")
                    } else {
                        wk.error(res.msg,'','$(".layui-btn").removeClass(\'layui-disabled\').removeAttr(\'disabled\');');
                    }
                }
            })
        });
    });
</script>
</body>
</html>